package org.daochong.lang;

import javax.sql.DataSource;

public class JdbcTemplateFactory {

	private static JdbcTemplateFactory instance = null;

	public static JdbcTemplateFactory getDefault() {
		return instance;
	}

	private ThreadLocal<JdbcTemplate> local = new ThreadLocal<JdbcTemplate>();

	private DataSource dataSource;

	private boolean ignoreMappingError = false;
	private boolean ignoreNullValue = false;
	private boolean showSql = false;
	private SQLBuilder builder;
	private boolean useBuilder = false;

	private String dateFormat = JdbcTemplate.DEFAULT_DATE_FORMAT;
	private String numberFormat = JdbcTemplate.DEFAULT_NUMBER_FORMAT;
	private String encoding = JdbcTemplate.DEFAULT_ENCODING;
	private String trueString = JdbcTemplate.DEFAULT_TRUE_STRING;
	private String falseString = JdbcTemplate.DEFAULT_FALSE_STRING;
	private int bigIntegerBits = JdbcTemplate.DEFAULT_BIGINTEGER_BITS;

	public SQLBuilder getBuilder() {
		return builder;
	}

	public void setBuilder(SQLBuilder builder) {
		this.builder = builder;
	}

	public boolean isUseBuilder() {
		return useBuilder;
	}

	public void setUseBuilder(boolean useBuilder) {
		this.useBuilder = useBuilder;
	}

	public String getDateFormat() {
		return dateFormat;
	}

	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}

	public String getNumberFormat() {
		return numberFormat;
	}

	public void setNumberFormat(String numberFormat) {
		this.numberFormat = numberFormat;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public String getTrueString() {
		return trueString;
	}

	public void setTrueString(String trueString) {
		this.trueString = trueString;
	}

	public String getFalseString() {
		return falseString;
	}

	public void setFalseString(String falseString) {
		this.falseString = falseString;
	}

	public int getBigIntegerBits() {
		return bigIntegerBits;
	}

	public void setBigIntegerBits(int bigIntegerBits) {
		this.bigIntegerBits = bigIntegerBits;
	}

	public JdbcTemplateFactory() {
		if (instance == null) {
			instance = this;
		}
	}

	public boolean isIgnoreMappingError() {
		return ignoreMappingError;
	}

	public void setIgnoreMappingError(boolean ignoreMappingError) {
		this.ignoreMappingError = ignoreMappingError;
	}

	public boolean isIgnoreNullValue() {
		return ignoreNullValue;
	}

	public void setIgnoreNullValue(boolean ignoreNullValue) {
		this.ignoreNullValue = ignoreNullValue;
	}

	public boolean isShowSql() {
		return showSql;
	}

	public void setShowSql(boolean showSql) {
		this.showSql = showSql;
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public JdbcTemplate getJdbcTemplate() {
		JdbcTemplate jdbcTemplate = local.get();
		if (jdbcTemplate == null) {
			jdbcTemplate = new JdbcTemplate(this.getDataSource());
			jdbcTemplate.setShowSql(this.isShowSql());
			jdbcTemplate.setIgnoreMappingError(this.isIgnoreMappingError());
			jdbcTemplate.setIgnoreNullValue(this.isIgnoreNullValue());
			jdbcTemplate.setBigIntegerBits(getBigIntegerBits());
			jdbcTemplate.setBuilder(getBuilder());
			jdbcTemplate.setDateFormat(getDateFormat());
			jdbcTemplate.setEncoding(getEncoding());
			jdbcTemplate.setFalseString(getFalseString());
			jdbcTemplate.setTrueString(getTrueString());
			jdbcTemplate.setNumberFormat(getNumberFormat());
			jdbcTemplate.setUseBuilder(isUseBuilder());
			local.set(jdbcTemplate);
		}
		return jdbcTemplate;
	}

}
